/**
 * @file STL_Path.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2020-09-21
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#include "STL_Path.h"
#include "STL_Text.h"

namespace STL
{
    std::string STL_Path::extname(std::string path)
    {
        size_t pos = path.find_last_of(".");
        if (pos == std::string::npos)
        {
            return "";
        }
        else
        {
            return path.substr(pos + 1);
        }
    }

    bool STL_Path::isAbsolute(std::string path)
    {
        return STL_Text::string_start_with(path, "/");
    }

    std::string STL_Path::join(std::vector<std::string> paths)
    {
        std::string ret = "";
        for (size_t i = 0; i < paths.size(); i++)
        {
            ret = STL_Path::join(ret, paths.at(i));
        }
        return ret;
    }

    std::vector<std::string> STL_Path::split(std::string str, std::string pattern)
    {
        std::string::size_type pos;
        std::vector<std::string> result;
        if (str.empty())
            return result;
        str += pattern;
        size_t size = str.size();

        for (size_t i = 0; i < size; i++)
        {
            pos = str.find(pattern, i);
            if (pos < size)
            {
                std::string s = str.substr(i, pos - i);
                result.push_back(s);
                i = (int)(pos + pattern.size() - 1);
            }
        }

        return result;
    }

    std::string STL_Path::join(std::string path1, std::string path2)
    {
        auto protocol = std::string("");
        auto protocol_end = path1.find(std::string("://"), 0);
        if (protocol_end != std::string::npos)
        {
            protocol = path1.substr(0, protocol_end);
            path1 = path1.substr(protocol_end + 3);
        }
        auto vec1 = split(path1, "/");
        auto vec2 = split(path2, "/");
        vec1.insert(vec1.end(), vec2.begin(), vec2.end());
        std::string ret = "";

        // TODO 处理“..”

        for (int i = 0; i < vec1.size(); i++)
        {
            auto name = vec1.at(i);
            if (i == 0)
            {
                ret = name;
            }
            else
            {
                if (name.empty() || name == ".")
                {
                    continue;
                }
                else
                {
                    ret.append(name);
                }
            }
            if (i != vec1.size() - 1)
            {
                ret.append("/");
            }
        }
        if (!protocol.empty())
        {
            ret = protocol + "://" + ret;
        }
        return ret;

        // if (path1.empty())
        // {
        //     return path2;
        // }
        // else if (path2.empty())
        // {
        //     return path1;
        // }
        // else
        // {
        //     bool path1Have = (path1.at(path1.size() - 1) == '/');
        //     bool path2Have = (path2.at(0) == '/');
        //     if (path1Have && path2Have)
        //     {
        //         return (path1 + path2.substr(1));
        //     }
        //     else if (!path1Have && !path2Have)
        //     {
        //         return (path1 + "/" + path2);
        //     }
        //     else
        //     {
        //         return (path1 + path2);
        //     }
        // }
    }
} // namespace STL
